import { Events } from "@/Constants";
import GridSystem from "@/GridSystem";
import Text from "../basic-shape/Text";
import Feature from "../Feature";

let timer: number = 0;
let _mouseDown = (e: any) => { }
export default class Cursor {

    static instance: Cursor | null = null;
    // 静态方法，用于获取单例实例  
    static getInstance(): Cursor {
        if (!Cursor.instance) {
            Cursor.instance = new Cursor();
        }
        Cursor.instance.blink();
        Feature.Gls.off(Events.MOUSE_DOWN, _mouseDown)
        _mouseDown = (e: any) => {
            const parent = Cursor.instance?.parent;
            if (parent) {
                if (Feature.Gls.getFocusNode() != parent) {
                    parent.dispatch(new CustomEvent(Events.BLUR, { detail: {} }))
                    Cursor.instance && (Cursor.instance.parent = null);
                } else {
                    parent.onEdit(e)
                }
            }
        }
        Feature.Gls.on(Events.MOUSE_DOWN, _mouseDown)
        return Cursor.instance;
    }

    x = 0;
    y = 0;
    width = 1.5;
    visible = true;
    parent: Text | null = null;

    _delFn = () => {
        this.toDel();
        this.blink();
    }
    _backspaceFn = () => {
        this.toBackspace();
        this.blink();
    }
    _leftFn = () => {
        this.toLeft();
        this.blink();
    }
    _rightFn = () => {
        this.toRight();
        this.blink();
    }
    _upFn = () => {
        this.toUp();
        this.blink();
    }
    _downFn = () => {
        this.toDown();
        this.blink();
    }
    _enterFn = () => {
        this.toEnter();
        this.blink();
    }

    constructor() {
        GridSystem.Shortcuts.addEvent(["del"], this._delFn)
        GridSystem.Shortcuts.addEvent(["backspace"], this._backspaceFn)
        GridSystem.Shortcuts.addEvent(["left"], this._leftFn)
        GridSystem.Shortcuts.addEvent(["right"], this._rightFn)
        GridSystem.Shortcuts.addEvent(["up"], this._upFn)
        GridSystem.Shortcuts.addEvent(["down"], this._downFn)
        GridSystem.Shortcuts.addEvent(["enter"], this._enterFn)
    }

    blink() {
        this.visible = true;
        timer && clearInterval(timer)
        timer = setInterval(() => {
            this.visible = !this.visible;
        }, 600)
    }

    setPos(x: number, y: number) {
        this.x = x;
        this.y = y;
    }

    draw(ctx: CanvasRenderingContext2D) {
        if (!this.visible || !this.parent) return
        const O = Feature.Gls.getPixelPos(Feature.getCenterPos(this.parent.pointArr));
        const angle = this.parent.angle;
        const fontSize = Feature.Render.getRatioSize(Feature.Render.getRelativeLen(this.parent.fontSize));
        ctx.save();
        ctx.beginPath();
        ctx.translate(O.x, O.y)
        ctx.rotate(angle * Math.PI / 180)
        ctx.translate(-O.x, -O.y)
        ctx.moveTo(this.x, this.y);
        ctx.lineTo(this.x, this.y + fontSize);
        ctx.strokeStyle = "black"
        ctx.setLineDash([])
        ctx.lineWidth = this.width;
        ctx.stroke();
        ctx.restore();
    }

    toBackspace() { }
    toLeft() { }
    toRight() { }
    toUp() { }
    toDown() { }
    toEnter() { }
    toDel() { }
}